"ui";
ui.layout(
    <frame w="*" h="*">
        <webview id="webView" layout_below="title" src="https://m.baidu.com"/>
    </frame>
);
// console.setSize(1000, 1000);
// console.show();
var url = "https://uua.jr.jd.com/uc-fe-wxgrowing/cloudpig/index/home/20191030/158432123100870350/?channelLV=qdy&shareId=vZoq-O6FkuUp62HuKheuTcAdoUJQ3Dik&sourceID=240&actflag=1AEE706F1D&isPay=N&utm_source=Android%2aurl%2a1584321232229&utm_medium=jrappshare&utm_term=qqfriends";
ui.run(() => {ui.webView.loadUrl(url)});
const codeList = [
    "/*网页调试工具*/\n(function () {\n    if (document.getElementById('mxvConsole2')) {\n    } else {\n        var version = '3.3.4';\n        function getCache(type) {\n            try {\n                const local = JSON.parse(window.localStorage[type]);\n                return local.data ? local : null;\n            } catch (e) {\n                return null;\n            }\n        }\n        function setCache(type, data) {\n            if (!data) {\n                return;\n            }\n            window.localStorage[type] = JSON.stringify({\n                version,\n                data\n            });\n        }\n        async function getVconsole() {\n            const cache = getCache('vconsole');\n            if (cache && cache.version == version) {\n                return cache.data;\n            }\n            return window.fetch('//cdn.bootcss.com/vConsole/' + version + '/vconsole.min.js').then(res => res.text())\n                .then(data => {\n                    setCache('vconsole', data);\n                    return data;\n                });\n        }\n        async function buildVconsole() {\n            const data = await getVconsole();\n            writeJs(data);\n            const vConsole = new VConsole();\n        }\n        function writeJs(text) {\n            var link = document.createElement('script');\n            link.innerHTML = text;\n            document.getElementsByTagName('head').item(0).appendChild(link);\n        }\n        function init() {\n            getVconsole();\n            buildVconsole();\n        }\n        init();\n    }\n    var pans = document.createElement('b');\n    pans.id = 'mxvConsole2';\n    document.body.appendChild(pans);\n})();"
    // "(function() {var oHead5415 = document.getElementsByTagName('body').item(0); var oScript5415 = document.createElement('script');        oScript5415.type = 'text/javascript';        oScript5415.src='https://greasyfork.org/scripts/398000-%E4%BA%AC%E4%B8%9C%E5%85%BB%E7%8C%AA/code/%E4%BA%AC%E4%B8%9C%E5%85%BB%E7%8C%AA.user.js';        oHead5415.appendChild( oScript5415);})();"
];

ui.webView.webViewClient = new JavaAdapter(android.webkit.WebViewClient, {
    onPageFinished: (webView, url)=>{
        toastLog("页面加载完成");
        codeList.forEach((code,index) =>{
            toastLog("加载脚本" + index);
            ui.webView.loadUrl("javascript: " + code);
        })
        ui.run(() => {
            let file = open('pig.js', mode = "r", encoding = "utf-8", bufferSize = 8192);
            let js = '';
            file.readlines().forEach(item => {
                // js += item + "\n\r";
                js += item;
            });
            file.close();
            ui.webView.loadUrl("javascript:" + js);
        })
    }
});
ui.webView.webChromeClient = new JavaAdapter(android.webkit.WebChromeClient, {
    onConsoleMessage: (msg) => {
        console.log("[%s:%s]: %s", msg.sourceId(), msg.lineNumber(), msg.message())
    }
});